package com.uenpay.im.ui.old.activity.label;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qyx.android.entity.FriendEntity;
import com.qyx.android.message.type.PacketType;
import com.uenpay.im.R;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.weight.RoundImageView;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2015-12-28 12:48:59
 * @copyRight http://www.qiyunxin.com
 * @function TODO
 */
public class LabelMemberAdapter extends BaseAdapter {

	private ArrayList<FriendEntity> arrayList = new ArrayList<FriendEntity>();
	private LayoutInflater layoutInflater;
	private boolean _isDeleteMode = false;

	public LabelMemberAdapter(Context _Context,
			ArrayList<FriendEntity> _ArrayLis) {
		this.arrayList = _ArrayLis;
		layoutInflater = LayoutInflater.from(_Context);
		addDel();
		notifyDataSetChanged();
	}

	private void addDel() {
		FriendEntity participant = new FriendEntity();
		arrayList.add(participant);
		arrayList.add(participant);
	}

	@Override
	public int getCount() {
		return arrayList == null ? 0 : arrayList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return arrayList == null ? null : arrayList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		labelViewHodler viewHodler = null;
		FriendEntity friendEntity = arrayList.get(arg0);
		if (arg1 == null) {
			viewHodler = new labelViewHodler();
			arg1 = layoutInflater.inflate(R.layout.item_label_member_layout,
					arg2,false);
			viewHodler.nameTv = (TextView) arg1.findViewById(R.id.name);
			viewHodler.avatarIv = (RoundImageView) arg1
					.findViewById(R.id.avatar);
			viewHodler.deleteMark = (ImageView) arg1
					.findViewById(R.id.delete_mark);
			arg1.setTag(viewHodler);
		} else {
			viewHodler = (labelViewHodler) arg1.getTag();
		}
		
		if(!TextUtils.isEmpty(friendEntity.remarks_name)){
			viewHodler.nameTv.setText(friendEntity.remarks_name);
		}else{
			viewHodler.nameTv.setText(friendEntity.nick_name);
		}
		if(TextUtils.isEmpty(friendEntity.cust_id)){
			if (arg0 == arrayList.size() - 1) {
				viewHodler.avatarIv.SetSrc(R.drawable.icon_delete);
			} else if (arg0 == arrayList.size() - 2) {
				viewHodler.avatarIv.SetSrc(R.drawable.icon_add);
			}
		}else{
			viewHodler.avatarIv.SetUrl(APIConfiguration.getAvatarUrlNormal(
					friendEntity.cust_id, PacketType.PERSONAL));
		}
		
		if(_isDeleteMode){
			viewHodler.deleteMark.setVisibility(View.VISIBLE);
			if (TextUtils.isEmpty(friendEntity.cust_id)) {
				viewHodler.avatarIv.setVisibility(View.INVISIBLE);
				viewHodler.deleteMark.setVisibility(View.INVISIBLE);
			}
		}else{
			viewHodler.avatarIv.setVisibility(View.VISIBLE);
			viewHodler.deleteMark.setVisibility(View.GONE);
		}
		return arg1;
	}

	private class labelViewHodler {
		private TextView nameTv;
		private ImageView deleteMark;
		private RoundImageView avatarIv;
	}

	public boolean isDeleteMode() {
		return _isDeleteMode;
	}

	public void setDeleteMode(boolean isDeleteMode) {
		_isDeleteMode = isDeleteMode;
		notifyDataSetChanged();
	}
}
